package band.wukong.practice.javase.core.thread.others;

public class TestNumberCharPrint {
    public static void main(String[] args) {
        Object o = new Object();
        Thread t1 = new NumberThread(o);
        Thread t2 = new CharThread(o);
        t1.start();
        t2.start();
    }

    private static class NumberThread extends Thread {
        Object o;

        public NumberThread(Object o) {
            super();
            this.o = o;
        }

        public void run() {
            synchronized (o) {
                for (int i = 1; i <= 26; i++) {
                    System.out.println("i = " + i);
//                    System.out.println(i * 2 - 1);
//                    System.out.println(i * 2);
                    o.notifyAll();
                    try {
                        if (i != 26)
                            o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    private static class CharThread extends Thread {
        Object o;

        public CharThread(Object o) {
            super();
            this.o = o;
        }

        public void run() {
            synchronized (o) {
                for (char c = 'A'; c <= 'Z'; c++) {
                    System.out.println("c = " + c);
                    o.notifyAll();
                    try {
                        if (c != 'Z')
                            o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
